<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router' 

const router = useRouter()
const prompt = ref('')
const generatedImage = ref('')
const loading = ref(false)
const error = ref('')

const generateImage = () => {
  if (!prompt.value.trim()) {
    error.value = '请输入图像描述'
    return
  }
  
  error.value = ''
  loading.value = true
  generatedImage.value = ''
  
  // 调用后端文生图接口
  fetch(`http://localhost:8080/ai/image?prompt=${encodeURIComponent(prompt.value)}`)
    .then(async response => {
      console.log('Response status:', response.status);
      // 检查响应状态
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }
      
      // 解析JSON响应
      const jsonData = await response.json();
      console.log('JSON response:', jsonData);
      
      // 根据实际的JSON结构解析图像URL
      if (jsonData.output && 
          jsonData.output.results && 
          Array.isArray(jsonData.output.results) && 
          jsonData.output.results.length > 0 &&
          jsonData.output.results[0].url) {
        // 从返回数据中提取图像URL
        generatedImage.value = jsonData.output.results[0].url;
      } else if (jsonData.output && jsonData.output.task_status === 'FAILED') {
        // 处理任务失败的情况
        throw new Error('图像生成任务失败');
      } else {
        // 无法识别的响应格式
        throw new Error('无法解析图像URL');
      }
    })
    .catch(err => {
      console.error('图像生成失败:', err);
      error.value = `图像生成失败: ${err.message}`;
    })
    .finally(() => {
      loading.value = false;
    })
}

const goToHome = () => {
  router.push({ name: 'home' });
}
</script>

<template>
  <div class="image-gen-container">
    <div class="image-gen-header">
      <h1 @click="goToHome" class="clickable-title">AI 文生图</h1>
      <p>生成图像内容的多模态模型</p>
    </div>
    
    <div class="prompt-input">
      <textarea 
        v-model="prompt" 
        placeholder="请输入图像描述，例如：一只在海边看日落的猫"
        rows="4"
      ></textarea>
      <button @click="generateImage">生成图像</button>
    </div>
    
    <div class="image-preview">
      <div v-if="generatedImage" class="image-container">
        <img :src="generatedImage" alt="生成的图像" />
      </div>
      <div v-else class="placeholder">
        <p>生成的图像将显示在这里</p>
      </div>
    </div>
  </div>
</template>




<style scoped lang="scss">
.image-gen-container {
  max-width: 800px;
  width: 100%;
  min-height: 100vh;
  margin: 0 auto;
  padding: 2rem;
  display: flex;
  flex-direction: column;
}

.image-gen-header {
  text-align: center;
  margin-bottom: 2rem;
  
  h1 {
    font-size: 2rem;
    color: #ed0808;
    margin-bottom: 0.5rem;
    cursor: pointer;
    
    &:hover {
      text-decoration: none;
    }
  }
  
  p {
    color: #666;
    font-size: 1rem;
  }
}

.prompt-input {
  margin-bottom: 2rem;
  
  textarea {
    width: 100%;
    padding: 1rem;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 1rem;
    font-family: inherit;
    resize: vertical;
    margin-bottom: 1rem;
    
    &:focus {
      outline: none;
      border-color: #409eff;
    }
  }
  
  button {
    padding: 0.8rem 1.5rem;
    background: #409eff;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 1rem;
    
    &:hover {
      background: #337ecc;
    }
  }
}

.image-preview {
  .image-container {
    text-align: center;
    
    img {
      max-width: 100%;
      border-radius: 8px;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }
  }
  
  .placeholder {
    background: #f5f5f5;
    border-radius: 8px;
    height: 300px;
    display: flex;
    align-items: center;
    justify-content: center;
    
    p {
      color: #999;
      font-size: 1.2rem;
    }
  }
}
</style>